<template>
	<xn-form-container
		:title="formData.id ? '编辑公司信息' : '增加公司信息'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="公司名称：" name="companyName">
						<a-input v-model:value="formData.companyName" placeholder="请输入公司名称" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="简称：" name="companyAbbreviation">
						<a-input v-model:value="formData.companyAbbreviation" placeholder="请输入简称" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="公司类型：" name="companyType">
						<a-select
							v-model:value="formData.companyType"
							:options="companyTypeOptions"
							allow-clear
							placeholder="请选择公司类型"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="地址：" name="companyAddress">
						<a-input v-model:value="formData.companyAddress" placeholder="请输入地址" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="联系电话：" name="companyTel">
						<a-input v-model:value="formData.companyTel" placeholder="请输入联系电话" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="联系人：" name="companyContact">
						<a-input v-model:value="formData.companyContact" placeholder="请输入联系人" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="服务期限：" name="companyLimited">
						<a-date-picker
							v-model:value="formData.companyLimited"
							value-format="YYYY-MM-DD HH:mm:ss"
							placeholder="请选择服务期限"
							style="width: 100%"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="公众号APPID：" name="companyAppid">
						<a-input v-model:value="formData.companyAppid" placeholder="请输入公众号APPID" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="公众号APPSECRET：" name="companyAppsecret">
						<a-input v-model:value="formData.companyAppsecret" placeholder="请输入公众号APPSECRET" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="备注：" name="remark">
						<a-input v-model:value="formData.remark" placeholder="请输入备注" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="homeCompanyForm">
import { cloneDeep } from 'lodash-es'
import { required } from '@/utils/formRules'
import homeCompanyApi from '@/api/home/setting/homeCompanyApi'
// 抽屉状态
const open = ref(false)
const emit = defineEmits({ successful: null })
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)
// 公司类型字典
let companyTypeOptions = [{label:'供应链平台',value:'0'},{label:'供应商',value:'1'},{label:'采购商',value:'2'}]
// 打开抽屉
const onOpen = (record) => {
	open.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
	}
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	open.value = false
}
// 默认要校验的
const formRules = {
	companyName: [required('请输入公司名称')],
	companyAbbreviation: [required('请输入简称')],
	companyTel: [required('请输入联系电话')],
	companyLimited: [required('请选择服务期限')],
	companyType: [required('请选择公司类型')]
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			homeCompanyApi
				.homeCompanySubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
		.catch(() => {})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
